package com.item.model.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.item.model.entity.WxOrder;
import com.item.model.entity.WxOrderDetail;
import com.item.model.entity.dto.LayuiListPage;
import com.item.model.entity.result.ResultVo;
import com.item.model.entity.vo.WxOrderVo;
import com.item.model.mapper.WxOrderDetailMapper;
import com.item.model.mapper.WxOrderMapper;
import com.item.model.service.WxOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.item.model.util.JSONUtil;
import com.item.model.util.ResultVoUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxOrderServiceImpl extends ServiceImpl<WxOrderMapper, WxOrder> implements WxOrderService {

    @Autowired
    private WxOrderMapper wxOrderMapper;


    @Transactional
    @Override
    public ResultVo addOrder(JSONObject order) {

        String order_no = RandomStringUtils.randomAlphabetic(15);
        order.put("order_no",order_no);
        order.put("create_order_time",new Date());
        WxOrder wxOrder = JSONObject.toJavaObject(order, WxOrder.class);
        int i = wxOrderMapper.insert(wxOrder);
        if (i<1) return ResultVoUtil.error("订单保存失败");
        System.out.println("------订单保存ID----"+wxOrder.getId());
        return ResultVoUtil.success("订单保存成功",wxOrder.getOrderNo());
    }

    @Override
    public ResultVo getPageList(Map<String, Object> params) {

        Integer page = (Integer) params.get("page");
        Integer limit = (Integer) params.get("limit");
        Page<List<WxOrderVo>> dataPage = new Page<>(page,limit);

        LayuiListPage<WxOrderVo> listPage = getPage(params, dataPage);
        return ResultVoUtil.success(listPage);
    }

    private LayuiListPage<WxOrderVo> getPage(Map<String, Object> params, Page<List<WxOrderVo>> dataPage) {
        List<WxOrderVo> cardfaceList = wxOrderMapper.getPageList(dataPage, params);
        LayuiListPage<WxOrderVo> listPage = new LayuiListPage<>();
        if (cardfaceList.size() == 0) {
            listPage.setCode(1);
            listPage.setMsg("未查询到结果数据");
            listPage.setCount(0);
        } else {
            listPage.setCode(0);
            listPage.setCount(dataPage.getTotal());
            listPage.setData(cardfaceList);
        }
        return listPage;
    }

    @Override
    public ResultVo getOrderById(String orderNo) {
        WxOrder order = wxOrderMapper.getOrderById(orderNo);
        return ResultVoUtil.success(order);
    }

    @Override
    public List<WxOrderVo> downList(HashMap<String, Object> params) {
        return wxOrderMapper.getPageList(null, params);
    }
}
